/**
 * 
 */
package com.shengruan.user.jdbc;

import java.sql.Connection;
import java.util.List;

import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;
import com.shengruan.framework.jdbc.BeanListHandler;
import com.shengruan.user.vo.AutoAuction;

/**
 * @author liubin
 *
 */
public class AutoAuctionJdbc extends BaseJdbc{
	/**
	 * 保存
	 */
	public void save(AutoAuction autoAuction)
	{
		Connection conn = this.getConnection();
		String sql = "insert into AutoAuction(userId,productId,startPrice,bidNum,tactics,state,time) values(?,?,?,?,?,?,?)";
		try
		{
			this.execute(conn, sql,autoAuction.getUserId(),autoAuction.getProductId(),autoAuction.getStartPrice(),autoAuction.getBidNum(),autoAuction.getTactics(),autoAuction.getState(),autoAuction.getTime());
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
		
	}
	
	/***
	 * 更新
	 */
	public void update(AutoAuction autoAuction)
	{
		Connection conn = this.getConnection();
		String sql = "update AutoAuction set userId=?,productId=?,startPrice=?,bidNum=?,tactics=? where id=?";
		try
		{
			this.execute(conn, sql,autoAuction.getUserId(),autoAuction.getProductId(),autoAuction.getStartPrice(),autoAuction.getBidNum(),autoAuction.getTactics(),autoAuction.getId());
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
	}
	
	/**
	 * 根据用户和状态获得列表
	 */
	public List<AutoAuction> getList(int userId,int state)
	{
		Connection conn = this.getConnection();
		String sql = "select * from AutoAuction_View  where userId=? and state=? order by time desc";
		List<AutoAuction> autoAuctionList = null;
		try
		{
			autoAuctionList = this.query(conn, sql, new BeanListHandler<AutoAuction>(AutoAuction.class),userId,state);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
		return autoAuctionList;
	}
	
	/***
	 * 设置状态
	 */
	public void setState(int id,int state,int userId)
	{
		Connection conn = this.getConnection();
		String sql = "update AutoAuction set state=? where id=? and userId=?";
		try
		{
			this.execute(conn, sql,state,id,userId);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
	}
	
	/***
	 * 删除
	 */
	public void deleteById(int id,int userId)
	{
		Connection conn = this.getConnection();
		String sql = "delete from AutoAuction where id=? and userId=?";
		try
		{
			this.execute(conn, sql,id,userId);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
	}
	
	
	/***
	 * 删除 根据商品
	 */
	public void deleteByProductId(int productId)
	{
		Connection conn = this.getConnection();
		String sql = "delete from AutoAuction where productId=?";
		try
		{
			this.execute(conn, sql,productId);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
	}
	
	
	/***
	 * 根据ID获得
	 */
	public AutoAuction getById(int id)
	{
		Connection conn = this.getConnection();
		AutoAuction autoAuction = null;
		String sql = "select * from AutoAuction where id=?";
		try
		{
			autoAuction = this.query(conn, sql, new BeanHandler<AutoAuction>(AutoAuction.class),id);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
		return autoAuction;
	}
	
	/***
	 * 更新bidNum
	 */
	public void updateBidNum(int id)
	{
		Connection conn = this.getConnection();
		
		String sql = "update AutoAuction set bidNum=bidNum-1 where id=?";
		try
		{
			this.execute(conn, sql,id);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
	
	}
	
	/***
	 * 检查是否存在
	 */
	public boolean checkExsit(int id,int userId,int productId)
	{
		Connection conn = this.getConnection();
		AutoAuction autoAuction = null;
		String sql = "select * from AutoAuction where userId=? and productId=? and id!=?";
		try
		{
			autoAuction = this.query(conn, sql, new BeanHandler<AutoAuction>(AutoAuction.class),userId,productId,id);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
		if(autoAuction==null)
			return false;
		else
			return true;
	}
	
	
	/***
	 * 根据ID获得
	 */
	public AutoAuction getByProductId(int userId,int productId)
	{
		Connection conn = this.getConnection();
		AutoAuction autoAuction = null;
		String sql = "select * from AutoAuction where userId=? and productId=?";
		try
		{
			autoAuction = this.query(conn, sql, new BeanHandler<AutoAuction>(AutoAuction.class),userId,productId);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			this.close(conn);
		}
		return autoAuction;
	}
}
